<template>
	<view :class="[BackGround]" :style="[{'font-weight':Style,'font-family':Font,minHeight:height + 'px',height: 100 + '%'}]">
		<cu-custom bgColor="bg-gradual-blue" isBack="true">
			<view slot="backText">返回</view>
			<view slot="content">修理管理详情</view>
		</cu-custom>
		<view :class="['margin-xs radius shadow shadow-lg bg-white',BackGroundBox]" v-if="isShow">
			<view style="margin-left: 32rpx;font-size: 36rpx;color: rgb(127, 184, 178);padding-top: 20rpx;">基本信息</view>
			<form @submit="formSubmit" @reset="formReset">
				<view class="cu-form-group">
					<view class="title">机器类别</view>
					<input type="text" disabled :value="deliverplan[0].LPRCTYPE" name="LMACHINETYPE" />
				</view>
				<view class="cu-form-group">
					<view class="title">机器型号</view>
					<input type="text" disabled :value="deliverplan[0].LMACHINETYPE" name="LMACHINETYPE" />
				</view>
				<view class="cu-form-group">
					<view class="title">制单人</view>
					<input type="text" disabled :value="deliverplan[0].LMAKEOPNAME" name="LMAKEOPID" />
				</view>
				<view class="cu-form-group">
					<view class="title">制单日期</view>
					<input disabled name="LMAKEDATE" type="text" :value="deliverplan[0].LMAKEDATE" />
				</view>
				<view class="cu-form-group">
					<view class="title">备注</view>
					<input type="text" name="LREMARK" :adjust-position="false" v-model="deliverplan[0].LREMARK" @focus="focus" />
				</view>
				<button-father :Position="'tb-fa-fixed'">
					<button class="cu-btn bg-gradual-red  lg shadow tb-flex cu-btn-new" @tap="deleteall">删除</button>
					<button class="cu-btn bg-gradual-blue  lg shadow tb-flex cu-btn-new" form-type="submit">确认</button>
				</button-father>
			</form>
		</view>
		<view :class="['bg-cyan solid-bottom margin-xs radius shadow shadow-lg',BackGroundBox]" style="background-color:#FFFFFF">
			<view style="margin-left: 32rpx;font-size: 36rpx;color: rgb(127, 184, 178);padding-top: 20rpx;">明细信息</view>
			<view class="grid text-center col-2" style="height: 100rpx;line-height: 100rpx;color: #888; border-bottom: 4rpx solid #e5e5e5;">
				<view class="">总油量:{{LREPWEIGHT}}</view>
				<view class="">总周期:{{LREPCIRCLE}}</view>
			</view>
			<view class="flex solid-bottom padding-sm justify-center" style="align-items: center;">
				<image @tap="jumpadddetail" style="width: 80rpx;height: 80rpx;" src="/static/images/add.png" mode="aspectFit"></image>
				<text class="text-center text-black padding-left-sm">添加信息</text>
			</view>
		</view>
		<view class="margin-xs radius shadow shadow-lg bg-white" v-if="isShow" style="margin-bottom: 100rpx;">
			<view class="cu-list menu-avatar">
				<view class="cu-item" :class="modalName=='move-box-'+ index?'move-cur':''" v-for="(item,index) in list" :key="index"
				 @touchstart="ListTouchStart" @touchmove="ListTouchMove" @touchend="ListTouchEnd" @tap="jumpedit(item,index)"
				 :data-target="'move-box-' + index">
					<view class="grid col-1 padding-sm" style="width: 100%;">
						<view class=" flex">
							<view class="text-grey padding-sm flex-sub">位置:{{item.LLREPPOSITION}}</view>
							<view class="text-gray padding-sm text-sm flex-sub">方式:{{item.LLREPTYPPE}}</view>
						</view>
						<view class=" flex">
							<view class="text-grey padding-sm flex-sub">供应商:{{item.LLPTNCODE}}</view>
							<view class="text-gray padding-sm text-sm flex-sub">备注:{{item.LLTSREMARK}}</view>
						</view>
					</view>
					<view class="move" @tap.stop="deletelseq(item,index)">
						<view class="bg-red">删除</view>
					</view>
				</view>
			</view>
		</view>
		<!--输入框组件-->
		<zyt-input tui-top-dropdown="tui-top-dropdown" bgcolor="rgba(76, 76, 76, 0.95)" :show="menuShow" :height="0" @close="closeMenu"
		 :zvalue="deliverplan[0].LREMARK" @zblur="zblur" v-if="isShow">
		</zyt-input>
		<!---输入框组件-->
	</view>
</template>

<script>
	import ButtonFather from '@/colorui/components/ButtonFather.vue'
	import utils from '@/static/js/utils.filter.js';
	import {
		mapGetters,
		mapMutations
	} from 'vuex'
	import {
		getSelect,
		getselectForm1
	} from "@/service/selectList.js"
	import {
		postinsert
	} from "@/service/insert"
	import {
		postupdate
	} from "@/service/updateList.js"
	import {
		postdeleteList
	} from "@/service/deleteList"
	export default {
		data() {
			return {
				date: global.dateright,
				height: this.screenHeight,
				isShow: false,
				flag: false,
				dateleft: global.dateleft,
				dateright: global.dateright,
				lmobile: global.userinfo.lmobile,
				LMAKEOPID: global.userinfo.lusername,
				modalName: null,
				LID:"",
				listTouchStart: 0,
				LREPWEIGHT:0,
				LREPCIRCLE:0,
				deliverplan:[],
				list: [],
				menuShow: null
			}
		},
		computed: {
			...mapGetters(['Style', 'Font','getlubriclist', 'periodgetlist','periodgetitem','BackGround', 'BackGroundBox'])
		},
		components: {
			ButtonFather
		},
		onUnload() {
			this.dellubriclist([])
		},
		onShow() {
			console.log(this.getlubriclist);
			if (this.getlubriclist) {
				
				this.list = this.getlubriclist
			}
		},
		onLoad(option) {
			this.LID = option.LID
			this.getSelect()
		},
		methods: {
			...mapMutations(['dellubriclist','lubricdellist']),
			async getSelect() {
				let { data } = await getSelect(9556,this.dateleft, this.dateright,{})
				if(data.length > 0)  {
					this.deliverplan = data.filter(item => {
						return item.LID == this.LID
					})
					for (let item of this.deliverplan) {
						
						var thisTime = item.LMAKEDATE.replace("T", " ").substring(0, 19).replace(/-/g, '/');
						item.LMAKEDATE = utils.timeTodate('Y/m/d', new Date(thisTime).getTime() + 8 * 3600 * 1000)
						this.isShow = true
					}
					if(this.deliverplan.length == 1) {
						if(this.deliverplan[0].LSEQ) {
							this.list = this.deliverplan
							// this.LREPWEIGHT += parseInt(this.deliverplan[0].LLREPWEIGHT)
							// this.LREPCIRCLE += parseInt(this.deliverplan[0].LLREPCIRCLE) 
							this.dellubriclist(this.deliverplan)
						}
					} else {
						
						this.list = this.deliverplan
						this.dellubriclist(this.deliverplan)
					}
				}
				
			},
			PickerChange1(e) {
				this.index1 = e.detail.value
			},
			focus(e) {
				this.menuShow = true
				this.deliverplan[0].LREMARK = e.detail.value
			},
			openMenu() {
				this.menuShow = false
			},
			zblur(value) {
				this.deliverplan[0].LREMARK = value
			},
			closeMenu() {
				this.menuShow = false
			},
			deleteall() {
				postdeleteList(9556,this.LID,'','').then(res =>{
					if(res.code == 1) {
						setTimeout(() => {
							uni.navigateBack({
								delta:1
							})
						},500)
						uni.showToast({
							title:"删除成功",
							icon:'success'
						})
					}
				})
			},
			deletelseq(e,index) {
				console.log(index);
				postdeleteList(9556,this.LID,e.LSEQ,'').then(res =>{
					if(res.code == 1) {
						console.log(this.list.length);
						if(this.list.length == 1) {
							this.list = []
							this.LREPWEIGHT = 0
							this.LREPCIRCLE = 0
							this.dellubriclist([])
						} else {
							this.list.splice(index,1)
							this.LREPWEIGHT = 0
							this.LREPCIRCLE = 0
							this.list.forEach((val,index) => {
								this.LREPWEIGHT += parseInt(val.LLREPWEIGHT) 
								this.LREPCIRCLE += parseInt(val.LLREPCIRCLE) 
							})
							// this.lubricdellist(index)
						}
						uni.showToast({
							title:"删除成功",
							icon:'success'
						})
					} else {
						uni.showToast({
							title:"删除失败",
							icon:'none'
						})
					}
				})
			},
			formSubmit(e) {
				console.log(e);
				var rule = [];
				//进行表单检查
				var formData = e.detail.value;
				var checkRes = this.$graceChecker.check(formData, rule);
				console.log(this.list);
				if (this.list == '') {
					uni.showToast({
						title: '请添加从表',
						icon: 'none'
					})
				} else if (!checkRes) {
					uni.showToast({
						title: this.$graceChecker.error,
						icon: "none"
					});
				} else {
					console.log(e.detail.value);
					// e.detail.value['LPRCTYPE'] = this.picker1[this.index1].lname
					e.detail.value['LTYPE'] = 0
					e.detail.value['LID'] = this.LID
					e.detail.value['LSTATERUN'] = 0
					let data = e.detail.value
					postupdate(955601, global.userinfo.lusercode, {
						"table":e.detail.value
					})
					this.list.forEach((value,index) => {
						delete value['LMACHINENO']
						delete value['LMACHINETYPE']
						delete value['LMAKEDATE']
						delete value['LMAKEOPID']
						delete value['LMAKEOPNAME']
						delete value['LPRCTYPE']
						delete value['LREMARK']
						delete value['isShow']
						delete value['LSTATERUN']
						delete value['LTYPE']
						delete value['isShow']
					})
					console.log(this.list);
					postinsert(955601,global.userinfo.lusercode,{"table":data,"dts":this.list}).then(res => {
						if(res.code == 1) {
							uni.navigateBack({
								delta: 1
							})
							uni.showToast({
								title: '添加成功',
								icon: 'success',
								duration: 3000
							})
						} else {
							uni.showToast({
								title: '添加失败',
								icon: 'none',
							})
						}
					}).catch(err => {
						uni.showToast({
							title: '添加失败',
							icon: 'none',
						})
					})
				}
			},
			// 跳转添加从表页面
			jumpadddetail() {
				global.deliverplan = this.deliverplan[0]
				uni.navigateTo({
					url: './Sizerecordadd/Sizerecordadddetail'
				})
			},
			//跳转编辑从表页面
			jumpedit(item, index) {
				uni.navigateTo({
					url: `./Sizerecordadd/Sizerecordset?item=${JSON.stringify(item)}&index=${index}&LID=${this.LID}`
				})
			},
			// ListTouch触摸开始
			ListTouchStart(e) {
				this.listTouchStart = e.touches[0].pageX
			},
			// ListTouch计算方向
			ListTouchMove(e) {
				this.listTouchDirection = e.touches[0].pageX - this.listTouchStart > 0 ? 'right' : 'left'
			},

			// ListTouch计算滚动
			ListTouchEnd(e) {
				if (this.listTouchDirection == 'left') {
					this.modalName = e.currentTarget.dataset.target
				} else {
					this.modalName = null
				}
				this.listTouchDirection = null
			}
		}
	}
</script>

<style>
	page {
		background-color: whitesmoke;
	}
</style>
